//
//  RegisterOneCollectionViewCell.m
//  MingJiangApp
//
//  Created by wangliang on 15/3/11.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "RegisterOneCollectionViewCell.h"
#import "BCBaseObject.h"

@interface RegisterOneCollectionViewCell ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;

@end

@implementation RegisterOneCollectionViewCell

- (void)awakeFromNib {
    // Initialization code
    
    self.phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.phoneTextField.delegate = self;
}


- (IBAction)next:(id)sender {
    
    
    [self.phoneTextField resignFirstResponder];
    
    if (![BCBaseObject isMobileNumber:self.phoneTextField.text]) {
        
        [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"请输入正确的手机号"];
    }else {
        
        NSDictionary *dic = @{@"user_name":self.phoneTextField.text,@"type":@"0"};
//        [WLHttpRequestAfter userCheckUserWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        
//            if ([resultDictionary[@"status"] integerValue] == 1) {
        
//                [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"该手机号已被注册，请登录！"];
//            }else if ([resultDictionary[@"status"] integerValue] == 0) {
        
                [WLHttpRequestAfter userSendRangCodeWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
                    
                    if ([resultDictionary[@"status"] integerValue] == 0) {
                        
                        [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"发送失败"];
                    }else if ([resultDictionary[@"status"] integerValue] == 1) {

                        [[NSUserDefaults standardUserDefaults] setValue:resultDictionary[@"data"][@"code"] forKey:@"code"];
                        [[NSUserDefaults standardUserDefaults] setValue:self.phoneTextField.text forKey:@"user_name"];
                        [[NSUserDefaults standardUserDefaults] synchronize];
                        if ([self.delegate respondsToSelector:@selector(registerViewGoToNext:withObject:)]) {
                            
                            [self.delegate registerViewGoToNext:@"输入手机" withObject:self];
                        }
                    }
                    
                } andFailureBlock:^(NSError *resultError) {
                    
                }];
//            }
        
//        } andFailureBlock:^(NSError *resultError) {
//            
//        }];
    }
}

- (IBAction)goBack:(id)sender {
    
    if ([self.delegate respondsToSelector:@selector(registerViewGoToBack:withObject:)]) {
        
        [self.delegate registerViewGoToBack:@"" withObject:self];
    }
}

- (IBAction)cancel:(id)sender {
    
    if ([self.delegate respondsToSelector:@selector(registerViewCancel:withObject:)]) {
        
        [self.delegate registerViewCancel:@"" withObject:self];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if ([string isEqual:@""]) {
        
        return YES;
    }
    if (textField.text.length >= 11) {
        
        return NO;
    }
    return YES;
}


@end
